<template>
  <div>
    <!--消息提示-->

    <el-alert
      class="alert"
      :title="`数据一共有${this.$store.getters.liqiushiCounts}条`"
      type="info"
      show-icon
    >
    </el-alert>
    <!--  表格-->
    <el-table :data="randomsList" border style="width: 100%">
      <el-table-column prop="number" label="试题编号" width="120">
      </el-table-column>
      <el-table-column prop="subject" label="学科" width="120">
      </el-table-column>
      <el-table-column prop="catalog" label="目录" width="120">
      </el-table-column>
      <el-table-column label="题型" width="120">
        <template slot-scope="scope">
          <span>{{ renderList(scope.row.questionType, questionType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="题干" width="280">
        <template slot-scope="scope">
          <p v-html="scope.row.question"></p>
        </template>
      </el-table-column>
      <el-table-column label="录入时间" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.addDate | parseTimeByString }}</span>
        </template>
      </el-table-column>
      <el-table-column label="难度" width="80">
        <template slot-scope="scope">
          <span>{{ renderList(scope.row.difficulty, difficulty) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="creator" label="录入人" width="120">
      </el-table-column>
      <el-table-column label="审核状态" width="120">
        <template slot-scope="scope">
          <span>{{ renderList(scope.row.chkState, chkTypeLiqiushi) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="chkRemarks" label="审核意见" width="150">
      </el-table-column>
      <el-table-column prop="chkUser" label="审核人" width="120">
      </el-table-column>
      <el-table-column label="发布状态" width="150">
        <template slot-scope="scope">
          <span>{{
            maxListState(
              scope.row.chkState,
              chkTypeLiqiushi,
              scope.row.publishState,
              publishType
            )
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        style="text-align: center !important"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            @click="handlePreviewClick(scope.row)"
            type="text"
            size="small"
            >预览</el-button
          >

          <el-button
            :disabled="scope.row.chkState !== 0"
            @click="handleAuditClick(scope.row)"
            type="text"
            size="small"
            >审核
          </el-button>

          <el-button
            :disabled="scope.row.publishState !== 0"
            @click="handleModifyClick(scope.row)"
            type="text"
            size="small"
          >
            修改
          </el-button>
          <el-button
            @click="handleOnTheShelfClick(scope.row)"
            type="text"
            size="small"
          >
            {{ scope.row.publishState === 1 ? "下架" : "上架" }}
          </el-button>
          <el-button
            :disabled="scope.row.publishState !== 0"
            @click="handleDeletClick(scope.row)"
            type="text"
            size="small"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页器-->
    <el-row type="flex" justify="end">
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pagesize"
          layout=" prev, pager, next,sizes,jumper"
          :total="this.$store.getters.liqiushiCounts"
        >
        </el-pagination>
      </div>
    </el-row>

    <!--弹窗组件-->
    <!--预览弹层-->
    <preview-message-box
      :show-preview.sync="showPreview"
      :PreviewData="PreviewData"
    />
    <!--审核弹层-->
    <audit-message-box :showAudit.sync="showAudit" :row="row" />
  </div>
</template>

<script>
import { chkType, chkTypeLiqiushi, difficulty, publishType, questionType } from '@/api/hmmm/constants'
import PreviewMessageBox from '@/module-hmmm/components/components-liqiushi/previewMessageBox'
import AuditMessageBox from '@/module-hmmm/components/components-liqiushi/auditMessageBox'
import {
  deleteRandomsList,
  getOnTheShelfONE,
  getOnTheShelfZERO,
  updateQuestion,
  updateQuestionPlus
} from '@/api/hmmm/questions'

export default {
  name: 'questions-randoms-list-liqiushi',
  components: { AuditMessageBox, PreviewMessageBox },
  props: {
    randomsList: {
      type: Array,
      require: true
    }
  },
  data () {
    return {
      questionType,
      difficulty,
      chkType,
      publishType,
      chkTypeLiqiushi,
      // 分页
      currentPage: 1, // 初始页
      pagesize: 5, //    每页的数据
      total: 10,
      row: {}, // 其中row拿到的是对应一行的数据
      showPreview: false,
      showAudit: false, // 审核弹层组件
      PreviewData: {}// 预览弹层的数据

    }
  },
  computed: {},
  watch: {},
  created () {

  },
  mounted () {
  },
  methods: {
    /*
    * 分页器以及转换器以及状态修改
    * */
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pagesize = size
      console.log(this.pagesize) // 每页下拉显示数据
      this.$store.dispatch('questionsRandoms/getAsyncRandomsListParams', { pagesize: this.pagesize })
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage
      console.log(this.currentPage) // 点击第几页
      this.$store.dispatch('questionsRandoms/getAsyncRandomsListParams', { page: this.currentPage })
    },
    // 传入的是转换器 根据id来转出 label
    renderList (value, labels) {
      // console.log(value)
      const newList = labels.filter(item => item.value === value * 1)
      return newList[0].label
    },
    maxListState (shState, shLabels, sjState, sjLabels) {
      // console.log('@@@', shState, shLabels, sjState, sjLabels, '@@@')
      // 审核状态不为1 就是待审核和已拒绝  就是相当于发布状态为  待发布
      if (shState !== 1) {
        return '待发布'
      } else {
        if (sjState === 0) {
          return '已下架'
        } else {
          return '已发布'
        }
      }
    },

    /*
    *操作一栏的修改
    * */

    async handlePreviewClick (row) {
      // 其中row拿到的是对应一行的数据
      this.showPreview = true
      const { data } = await updateQuestionPlus(row)
      this.PreviewData = data
      console.log(data)
    },
    handleAuditClick (row) {
      // 其中row拿到的是对应一行的数据
      this.showAudit = true
      this.row = row
      // 拿到对应行的数据后只需要发送请求更改数据就行
      console.log(row)
    },
    async handleModifyClick (row) {
      console.log(row)
      // 当点击修改的时候 跳转页面 发送请求
      await this.$router.push('/questions/new')
      const data = {
        id: row.id
      }
      await updateQuestion(data)
      // 其中row拿到的是对应一行的数据
    },
    handleOnTheShelfClick (row) {
      console.log('liqiushi')
      Object.assign(row)
      // 其中row拿到的是对应一行的数据
      const publishText = row.publishState === 1 ? '下架' : '上架'
      this.$confirm(`您确认${publishText}这道题目吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        if (row.publishState === 1) {
          // 发送下架请求
          await getOnTheShelfZERO(row)
        } else {
          // 发送上架请求
          await getOnTheShelfONE(row)
        }
        await this.$store.dispatch('questionsRandoms/getAsyncRandomsListParams', {})
        this.$message({
          type: 'success',
          message: `${publishText}成功!`
        })
      }).catch(() => {

      })
    },
    handleDeletClick (row) {
      console.log(row)
      // 其中row拿到的是对应一行的数据
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteRandomsList(row)
        await this.$store.dispatch('questionsRandoms/getAsyncRandomsListParams', {})
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {

      })
    }
  }
}
</script>

<style scoped lang="scss">
.pagesplitter {
  margin-top: 20px;
  padding: 2px 5px;
}

.alert {
  margin: 0 0 15px;
  padding: 8px 16px;
}
</style>
